package com.workflow.action;

import java.util.Set;

import com.base.service.PermissionService;
import com.common.bean.Flowtemplatebranch;
import com.common.bean.Flowtemplatedetail;
import com.common.bean.Personnel;
import com.common.util.PagerModel;
import com.common.util.StringToken;
import com.core.ssh2.AbstractActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.workflow.bean.Applydept;
import com.workflow.bean.Applydeptdetail;
import com.workflow.bean.Outputdept;
import com.workflow.bean.Outputdeptdetail;
import com.workflow.service.ApplydeptService;
import com.workflow.service.ApplydeptdetailService;
import com.workflow.service.OutputdeptService;
import com.workflow.service.OutputdeptdetailService;

public class OutputdeptdetailManagerAction extends AbstractActionSupport
		implements Preparable {
	private static final long serialVersionUID = -2017713846120656676L;

	private OutputdeptdetailService outputdeptdetailService;
	
	private OutputdeptService outputdeptService;
	
	private ApplydeptService applydeptService;
	
	private ApplydeptdetailService applydeptdetailService;
	
	private PermissionService permissionService;
	
	private Applydept applydept;
	
	private Outputdeptdetail outputdeptdetail;
	
	private Outputdept outputdept;
	
	private String outdetailno;
	
	private String outputno;
	
	private PagerModel pm;
	
	private Set cardsubtypelist;
	
	public void prepare() throws Exception {
		
	}
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String goAddOutputdeptdetail() throws Exception {
		outputdept = outputdeptService.findOutputdept(outputno);
		outputdeptdetail.setOutputdept(outputdept);
		cardsubtypelist = outputdept.getCardmaintype().getCardsubtypes();
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String addOutputdeptdetail() throws Exception {
		Personnel personnel = (Personnel) getSession().get(StringToken.CURRENT_LOGIN);
		outputdept = outputdeptService.findOutputdept(outputno);
		outputdeptdetail.setOutputdept(outputdept);
		
		Object obj = null;
		Flowtemplatedetail flowtemplatedetail = outputdept.getFlowtemplatedetail();
		if(flowtemplatedetail.getSeq() == 1l) {
			//判断用户权限部门、角色
			if(flowtemplatedetail.getDepartment().equals(personnel.getDepartment())) {
				long ruleno = flowtemplatedetail.getRule().getRuleno();
				String staffcode = personnel.getStaffcode();
				
				obj = permissionService.findPersonneltorulelink(ruleno, staffcode);
			}
		}
		if(!"admin".equals(personnel.getLoginname()))
		if(obj == null) { //部门或者角色权限至少有一个不符合
			throw new RuntimeException("you have no permission for this deal.");
		}
		
		if(StringToken.DICT_FLOWBRANCH_YES.equals(outputdept.getDatadictByBranch().getName())){
			Set<Flowtemplatebranch> set = outputdept.getFlowtemplatedetail().getFlowtemplatebranchs();
			for(Flowtemplatebranch flowtemplatebranch : set) {
				if(StringToken.DICT_FLOWBRANCH_DEF.equals(flowtemplatebranch.getDefaultrun())){//保存默认分支
					outputdeptdetail.setFlowtemplatebranch(flowtemplatebranch);
				}
				if("1".equals(flowtemplatebranch.getBranchseq())) {
					if(outputdeptdetail.getCardsubtype().equals(flowtemplatebranch.getCardsubtype())) {
						outputdeptdetail.setFlowtemplatebranch(flowtemplatebranch);
					}
				}
			}
		}
		outputdeptdetailService.addOutputdeptdetail(outputdeptdetail);
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String addOutputdeptdetailByApply() throws Exception {
		Personnel personnel = (Personnel) getSession().get(StringToken.CURRENT_LOGIN);
		outputdept = outputdeptService.findOutputdept(outputno);
		//获取报领单号
		String applyno = outputdept.getUpperflowno();
		applydept = applydeptService.findApplydept(applyno);
		//获取报领明细
		Object obj = null;
		Flowtemplatedetail flowtemplatedetail = outputdept.getFlowtemplatedetail();
		if(flowtemplatedetail.getSeq() == 1l) {
			//判断用户权限部门、角色
			if(flowtemplatedetail.getDepartment().equals(personnel.getDepartment())) {
				long ruleno = flowtemplatedetail.getRule().getRuleno();
				String staffcode = personnel.getStaffcode();
				
				obj = permissionService.findPersonneltorulelink(ruleno, staffcode);
			}
		}
		if(!"admin".equals(personnel.getLoginname()))
		if(obj == null) { //部门或者角色权限至少有一个不符合
			throw new RuntimeException("you have no permission for this deal.");
		}
		
		Set<Applydeptdetail> applydeptdetailSet = applydept.getApplydeptdetails();
		for(Applydeptdetail applydeptdetail : applydeptdetailSet) {
			Outputdeptdetail outputdetail = new Outputdeptdetail();
			outputdetail.setCardsubtype(applydeptdetail.getCardsubtype());
			outputdetail.setTotalsum(applydeptdetail.getTotalsum());
			outputdetail.setTotalmoney(applydeptdetail.getMoney());
			outputdetail.setOutputdept(outputdept);
			if(StringToken.DICT_FLOWBRANCH_YES.equals(outputdept.getDatadictByBranch().getName())){
				Set<Flowtemplatebranch> set = outputdept.getFlowtemplatedetail().getFlowtemplatebranchs();
				for(Flowtemplatebranch flowtemplatebranch : set) {
					if(StringToken.DICT_FLOWBRANCH_DEF.equals(flowtemplatebranch.getDefaultrun())){//保存默认分支
						outputdetail.setFlowtemplatebranch(flowtemplatebranch);
					}
					if("1".equals(flowtemplatebranch.getBranchseq())) {
						if(outputdetail.getCardsubtype().equals(flowtemplatebranch.getCardsubtype())) {
							outputdetail.setFlowtemplatebranch(flowtemplatebranch);
						}
					}
				}
			}
			outputdeptdetailService.addOutputdeptdetail(outputdetail);
		}
		
		return SUCCESS;
	}
	
	public String findAllOutputdeptdetail() throws Exception {
		outputdept = outputdeptService.findOutputdept(outputno);
		pm = outputdeptdetailService.findOutputdeptdetailForPager(outputno);
		return SUCCESS;
	}
	
	public String findOutputdeptdetail() throws Exception {
		outputdeptdetail = outputdeptdetailService.findOutputdeptdetail(outdetailno);
		return SUCCESS;
	}
	
	public String delOutputdeptdetail() throws Exception {
		outputdeptdetailService.removeOutputdeptdetail(outdetailno);
		return SUCCESS;
	}
	
	public String getOutputdeptdetailForUpdate() throws Exception {
		outputdeptdetail = outputdeptdetailService.findOutputdeptdetail(outdetailno);
		return SUCCESS;
	}
	
	public String updateOutputdeptdetail() throws Exception {
		outputdeptdetailService.updateOutputdeptdetail(outputdeptdetail);
		return SUCCESS;
	}

	public Set getCardsubtypelist() {
		return cardsubtypelist;
	}

	public void setCardsubtypelist(Set cardsubtypelist) {
		this.cardsubtypelist = cardsubtypelist;
	}

	public String getOutdetailno() {
		return outdetailno;
	}

	public void setOutdetailno(String outdetailno) {
		this.outdetailno = outdetailno;
	}

	public Outputdept getOutputdept() {
		return outputdept;
	}

	public void setOutputdept(Outputdept outputdept) {
		this.outputdept = outputdept;
	}

	public Outputdeptdetail getOutputdeptdetail() {
		return outputdeptdetail;
	}

	public void setOutputdeptdetail(Outputdeptdetail outputdeptdetail) {
		this.outputdeptdetail = outputdeptdetail;
	}

	public OutputdeptdetailService getOutputdeptdetailService() {
		return outputdeptdetailService;
	}

	public void setOutputdeptdetailService(
			OutputdeptdetailService outputdeptdetailService) {
		this.outputdeptdetailService = outputdeptdetailService;
	}

	public OutputdeptService getOutputdeptService() {
		return outputdeptService;
	}

	public void setOutputdeptService(OutputdeptService outputdeptService) {
		this.outputdeptService = outputdeptService;
	}

	public String getOutputno() {
		return outputno;
	}

	public void setOutputno(String outputno) {
		this.outputno = outputno;
	}

	public PagerModel getPm() {
		return pm;
	}

	public void setPm(PagerModel pm) {
		this.pm = pm;
	}

	/**
	 * @return the applydept
	 */
	public Applydept getApplydept() {
		return applydept;
	}

	/**
	 * @param applydept the applydept to set
	 */
	public void setApplydept(Applydept applydept) {
		this.applydept = applydept;
	}

	/**
	 * @return the applydeptdetailService
	 */
	public ApplydeptdetailService getApplydeptdetailService() {
		return applydeptdetailService;
	}

	/**
	 * @param applydeptdetailService the applydeptdetailService to set
	 */
	public void setApplydeptdetailService(
			ApplydeptdetailService applydeptdetailService) {
		this.applydeptdetailService = applydeptdetailService;
	}

	/**
	 * @return the applydeptService
	 */
	public ApplydeptService getApplydeptService() {
		return applydeptService;
	}

	/**
	 * @param applydeptService the applydeptService to set
	 */
	public void setApplydeptService(ApplydeptService applydeptService) {
		this.applydeptService = applydeptService;
	}

	/**
	 * @return the permissionService
	 */
	public PermissionService getPermissionService() {
		return permissionService;
	}

	/**
	 * @param permissionService the permissionService to set
	 */
	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}
	
}
